package com.bijiawang.user.controller;

import com.bijiawang.user.bean.History;
import com.bijiawang.user.domin.Favorites;
import com.bijiawang.user.service.FavoritesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class HelloController {

    @Autowired
    FavoritesService favoritesService;

    @GetMapping("/favorites/{pageNum}")
    public String favorites(@AuthenticationPrincipal UserDetails userDetails, Model model,@PathVariable("pageNum")int pageNum){
        List<Favorites> allList = favoritesService.getFavorites(userDetails.getUsername());
        model.addAttribute("alls",allList);
        List<Favorites> xmArrayList = null;
        int count = 0;
        int lines = 15;
        if (allList.size() != 0) {
            count = allList.size() % lines == 0 ? allList.size() / lines : allList.size() / lines + 1; //页数
            if (pageNum == count) {
                xmArrayList = allList.subList(lines * (pageNum - 1), allList.size());
            } else {
                xmArrayList = allList.subList(lines * (pageNum - 1), lines * pageNum);
            }
        }
        model.addAttribute("emps",xmArrayList);
        model.addAttribute("pagenum",pageNum);
        model.addAttribute("count",count);
        return "Favorites";
    }
    @RequestMapping("/historyPrice")
    public String historyPrice(@RequestParam("Id") int Id, Model model){
        model.addAttribute("id",Id);
        return "historyPrice";
    }

    @RequestMapping("/deleteFavorites")
    public String deleteFavorites(@RequestParam("Id") int id){
        favoritesService.deleteFavorites(id);
        return "redirect:/favorites/1";
    }
}
